#!/usr/bin/env bash
# Automatically update versions in files and commit changes

set -e

NEW_VERSION=$1

if [ -z "$NEW_VERSION" ]; then
    NEW_VERSION=$(jq -r ".version" manifest-beta.json | awk -F. -v OFS=. '{$NF += 1 ; print}')
fi

echo "Releasing beta version '${NEW_VERSION}'"

# Delete old files if they exist
rm -f package.tmp.json
rm -f manifest-beta.tmp.json

# Rewrite versions in relevant files.
jq ".version=\"${NEW_VERSION}\"" package.json > package.tmp.json && mv package.tmp.json package.json
jq ".version=\"${NEW_VERSION}\"" manifest-beta.json > manifest-beta.tmp.json && mv manifest-beta.tmp.json manifest-beta.json

# Create commit & commit.
git commit -a -m "Auto-release beta ${NEW_VERSION}"
git push

